<?php




/**
 * 用户列表
 *
 */
function li($page_size = ADM_PAGESIZE) {
    global $_TABLES, $class_var, $timestamp;

    $view_var   = array();
    $tbl_faq    = $_TABLES['xmyx'];
    $conn   = & get_db_conn();


    $where      = 'WHERE 1 ';
    // 关键字
    $key    = trim(@$_GET['key']);
    if($key != '') $where  .= "AND (lxname LIKE '%$key%' OR content LIKE '%$key%' OR lxphone LIKE '%$key%' )";

    // 分类
    $aid    = @(int)$_GET['aid'];
   
    if($aid) {
        $where  .= "AND aid = $aid ";
    }

    // 发布时间
/*    $begin_date = @$_GET['begin_date'];
    $end_date   = @$_GET['end_date'];
    if($begin_date == '') {
        $begin_date   = 0;
    } else {
        $tmp    = explode('-', $begin_date);
        $begin_date = mktime(0 ,0, 0,$tmp[1] ,$tmp[2], $tmp[0]);
    }
    if($end_date == '') {
        $end_date   = $timestamp;
    } else {
        $tmp        = explode('-', $end_date);
        $end_date   = mktime(23 ,59, 59,$tmp[1] ,$tmp[2], $tmp[0]);
    }
    $where .= "AND (ctime >= '$begin_date' AND ctime <= '$end_date' ) ";
*/

    // 分页处理开始
    $csql   = "SELECT COUNT(*) AS rs_count FROM $tbl_faq $where";
    $rs     = $conn->GetRow($csql);


    $record_total   = @(int)$rs['rs_count'];
    $page_total     = ceil($record_total/$page_size);

    $page   = @(int)$_GET['page'];
    if($page < 1) $page = 1;
    if($page > $page_total && $page_total > 0) $page = $page_total;
    $offset = ($page - 1) * $page_size;
    $limit  = "LIMIT $offset, $page_size";

    $view_var['record_total']   = $record_total;
    $view_var['page_total']     = $page_total;
    $view_var['page']           = $page;
    $view_var['page_size']      = $page_size;
    // 分页处理完毕


    // 列表处理开始
    $order_by   = trim(@$_GET['orderby']);
    $order_desc = @(int)$_GET['desc'];

    $str_order_by   = '';
    if($order_by != '') {
        $order_desc = ($order_desc!=1)?'ASC':'DESC';
        $str_order_by = "`{$order_by}` {$order_desc}, ";
    }

    
    $sql    = "SELECT * FROM $tbl_faq
            $where 
            ORDER BY  $str_order_by id DESC ";
//    echo $sql;
//	exit;
    $rs = $conn->PageExecute($sql,$page_size,$page);
    $view_var['record'] = $rs->GetRows();
    // 列表处理结束


    return $view_var;
}


function read($id = 0) {
    global $_TABLES, $timestamp;

    $view_var   = array();
    $tbl_faq    = $_TABLES['xmyx'];
    if($id > 0) {
        $conn   = & get_db_conn();
        $sql    = "SELECT * FROM $tbl_faq WHERE id = $id LIMIT 0,1 ";
        $view_var   = $conn->GetRow($sql);
    } else {
        $view_var   = array(
              'id'      => 0,
              'aid'     => 0,
              'lxname' => '',
              'content' => '',
              'lxphone'  => '',
              'lxqq' => 0,
              'display'     => 0,
              'ctime'      => '',
              'sip' => '',
            );
    }
    return $view_var;
}

function request() {
    global $action, $timestamp;

    $data   = $_POST['xmyx'];
    $data['id']     = @(int)$data['id'];
    $data['aid']    = @(int)$data['aid'];

    $data['display'] = @(int)$data['display'];
    if($data['display'] != 1) $data['display'] = 0;
    $data['reply'] = @(int)$data['reply'];
    if($data['reply'] != 1) $data['reply'] = 0;


    // 提问时间
    $data['ctime']  = $timestamp;
    // 回复之间
    $arr    = explode('-', $data['reply_date']);
    if(sizeof($arr) == 3) {
        $data['reply_hour'] = @(int)$data['reply_hour'];
        $data['reply_min']  = @(int)$data['reply_min'];
        $data['reply_sec']  = @(int)$data['reply_sec'];
        $data['replytime'] = mktime($data['reply_hour'] ,$data['reply_min'], $data['reply_sec'],$arr[1] ,$arr[2], $arr[0]);
    } else {
        $data['replytime'] = $timestamp;
    }

    // 过滤HTML内容中被php转义后的字符 
    // 去掉双引号前的转移符，将转义后的单引号转换成&#39;
    $data['content']    = str_replace("\\\"", "\"", str_replace("\\'", "&#39;", $data['content']));
    $data['answer']     = str_replace("\\\"", "\"", str_replace("\\'", "&#39;", $data['answer']));


    $rule   = array(
        'subject'   => 'i_string|len:1:100', 
        'content'   => 'i_string|len:1:-1',
        );

    // 验证正确性
    $def_err= array(
        'subject'   => '“问题标题”必须填写，且长度必须在1～100个字符之间！', 
        'content'   => '“问题内容”必须填写！', 
        );

    $result = data_validator($data, $rule, $def_err);
    if($result === true) {
        // 其他逻辑错误检验
    } else {
        foreach($result as $v) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                        $v, 
                        __FILE__,
                        __LINE__);
        }
    }

    if($GLOBALS['ERR']->Count > 0) {
        return false;
    } else {
        // 处理缩略图上传等问题
        return $data;
    }
}

function add() {
    global $_TABLES, $timestamp;

    $tbl_faq        = $_TABLES['faq'];
    $tbl_faq_class  = $_TABLES['article_class'];

    $result = request();
    if($result !== false) {
        $conn   = & get_db_conn();

        // 验证分类ID的合法性，如果不存在，分类ID=0
        if($result['cid'] > 0) {
            $sql = "SELECT COUNT(*) AS rs_count FROM $tbl_faq_class WHERE cid = '{$result['cid']}'";
            $rs  = $conn->GetRow($sql);
            if($rs['rs_count'] <= 0) $result['cid'] = $result['cata'];
        } else {
            $result['cid'] = $result['cata'];
        }

        // 先查看是否有同名记录
        $sql    = "SELECT COUNT(*) AS rs_count FROM $tbl_faq 
                        WHERE subject = '{$result['subject']}' 
                          AND cid = '{$result['cid']}' ";
        $rs     = $conn->GetRow($sql);
        if($rs['rs_count'] > 0) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                "系统当前分类中已经存在问题主题为<FONT COLOR=\"red\">{$result['subject']}</FONT>的问题，无法当前添加操作！", 
                __FILE__,
                __LINE__);
        } else {
            unset($result['id']); 
            $result['sendtime'] = $timestamp;
            if($conn->AutoExecute($tbl_faq,$result,'INSERT')) {
                $id    = $conn->Insert_ID();
                return $id;
            }
        }
    }
    return false;
}

function modify() {
    global $_TABLES, $timestamp;

    $tbl_faq        = $_TABLES['faq'];
    $tbl_faq_class  = $_TABLES['article_class'];

    $result = request();
    if($result !== false) {
        $conn   = & get_db_conn();

        // 验证分类ID的合法性，如果不存在，分类ID=0
        if($result['cid'] > 0) {
            $sql = "SELECT COUNT(*) AS rs_count FROM $tbl_faq_class WHERE cid = '{$result['cid']}'";
            $rs  = $conn->GetOne($sql);
            if(!$rs) $result['cid'] = $result['cata'];
        } else {
            $result['cid'] = $result['cata'];
        }

        // 先查看是否有同名记录
        $sql    = "SELECT COUNT(*) AS rs_count FROM $tbl_faq 
                        WHERE subject = '{$result['subject']}' 
                          AND cid = '{$result['cid']}' 
                          AND id <> '{$result['id']}' ";
        $rs     = $conn->GetRow($sql);
        if($rs['rs_count'] > 0) {
            $GLOBALS['ERR']->AddError(_ERROR_BUSINESS, 
                "系统当前分类中已经存在问题主题为<FONT COLOR=\"red\">{$result['subject']}</FONT>的问题，无法当前修改操作！", 
                __FILE__,
                __LINE__);
        } else {
            if($conn->AutoExecute($tbl_faq,$result,'UPDATE', 'id=' . $result['id'] )) {
                return $result['id'];
            }
        }
    }
}

function del() {
    global $_TABLES;
    $tbl_faq    = $_TABLES['xmyx'];

    $deltype    = trim(@$_POST['deltype']);
    if($deltype == 'all') {
        // 删除POST传递过来的 item_id 
        // 批量删除
        $str_in = array_join_comma($_POST['item_id']);
    } else {
        // get传递过来的删除 单条删除
        $item_id= @(int)$_GET['item_id'];
        $str_in = $item_id;
    }

    if($str_in != '') {
        $conn   = & get_db_conn();
        $sql    = "DELETE FROM $tbl_faq 
                    WHERE id IN ({$str_in}) ";
        if($conn->Execute($sql)) {
            js_success('删除指定内容成功！');
        }
    }
}

function getxmtitle($aid){
	 global $_TABLES;
    $tbl_faq    = $_TABLES['article'];
	
	$conn   = & get_db_conn();
    $sql    = "select article_title FROM $tbl_faq WHERE aid = $aid";
	$view_var   = $conn->GetRow($sql);
	return $view_var["article_title"];
}
?>